<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
        <el-form-item label="员工" prop="employeeId">

          <el-select v-model="queryParams.employeeId" placeholder="请选择员工" clearable>
            <el-option
              v-for="dict in employeeList"
              :key="dict.value"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="打卡类型" prop="punchType">
          <el-select v-model="queryParams.punchType" placeholder="请选择打卡类型" clearable>
            <el-option
              v-for="dict in punch_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="打卡时间" style="width: 308px">
          <el-date-picker
            v-model="daterangeCreateTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="mt20">

      <el-row :gutter="10" class="mb8" type="flex" justify="space-between">
        <el-col :span="6"><span style="font-size: large">考勤记录</span></el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['wms:punch:add']"
          >新增
          </el-button>
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['wms:punch:export']"
          >导出
          </el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="punchList" border class="mt20"
                :default-sort="{prop:['createTime'],order:'descending'}">
        <el-table-column label="员工" prop="employeeId">
          <template #default="scope">
            <span>{{ getEmployeeNameValue(scope.row.employeeId).name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="打卡类型" prop="punchType">
          <template #default="scope">
            <dict-tag :options="punch_status" :value="scope.row.punchType"/>
          </template>
        </el-table-column>
        <el-table-column label="定位信息" prop="location"/>
        <el-table-column label="打卡时间" align="center" prop="createTime" width="180" sortable>
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:punch:edit']">
              修改
            </el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                       v-hasPermi="['wms:punch:remove']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-row>
        <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-row>

    </el-card>
    <!-- 添加或修改考勤记录对话框 -->
    <el-drawer :title="title" v-model="open" size="50%" append-to-body>
      <el-form ref="punchRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工" prop="employeeId">
          <el-select v-model="form.employeeId" placeholder="请选择员工" clearable>
            <el-option
              v-for="dict in employeeList"
              :key="dict.value"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="打卡类型" prop="punchType">
          <el-select v-model="form.punchType" placeholder="请选择打卡类型">
            <el-option
              v-for="dict in punch_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="定位信息" prop="location">
          <el-input v-model="form.location" placeholder="请输入定位信息"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup name="Punch">
import {listPunch, getPunch, delPunch, addPunch, updatePunch} from "@/api/wms/punch";
import {getEmployeeName} from "@/api/wms/employees";

const {proxy} = getCurrentInstance();
const {punch_status} = proxy.useDict('punch_status');

const punchList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const daterangeCreateTime = ref([]);
const employeeList = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    employeeId: undefined,
    punchType: undefined,
    createTime: undefined,
  },
  rules: {
    id: [
      {required: true, message: "主键ID不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);


getEmployeeName().then(res => {
  employeeList.value = res.data;
});

//通过id值获取名字
function getEmployeeNameValue(employeeId) {
  const employee = employeeList.value.find(emp => emp.id === employeeId);
  return employee;
}

/** 查询考勤记录列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  listPunch(queryParams.value).then(response => {
    punchList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    employeeId: null,
    punchType: null,
    location: null,
    createTime: null,
    createBy: null,
    updateBy: null
  };
  proxy.resetForm("punchRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeCreateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加考勤记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getPunch(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改考勤记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["punchRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updatePunch(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addPunch(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除考勤记录编号为"' + _ids + '"的数据项？').then(function () {
    loading.value = true;
    return delPunch(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/punch/export', {
    ...queryParams.value
  }, `punch_${new Date().getTime()}.xlsx`)
}

getList();
</script>
